﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace AjaxControlFramework.Docs.Examples
{
    public enum ExampleLanguage : int
    {
        CSharp, VisualBasic
    }



    [ParseChildren(ChildrenAsProperties = true)]
    public class Example : Control
    {
        //------// Properties \\--------------------------------------------\\
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public virtual string Markup { get; set; }


        [PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual string CodeBehind { get; set; }


        [PersistenceMode(PersistenceMode.Attribute)]
        public virtual ExampleLanguage Language { get; set; }


        private string _markupLabel = "Markup"; // Default value.
        [PersistenceMode(PersistenceMode.Attribute)]
        public virtual string MarkupLabel
        {
            get { return _markupLabel; }
            set { _markupLabel = value; }
        }


        private string _markupCssClass = "markup"; // Default value.
        [PersistenceMode(PersistenceMode.Attribute)]
        public virtual string MarkupCssClass
        {
            get { return _markupCssClass; }
            set { _markupCssClass = value; }
        }


        private string _codeBehindLabel = "Code-Behind"; // Default value.
        [PersistenceMode(PersistenceMode.Attribute)]
        public virtual string CodeBehindLabel
        {
            get { return _codeBehindLabel; }
            set { _codeBehindLabel = value; }
        }


        private string _codeBehindCssClass = "code_behind"; // Default value.
        [PersistenceMode(PersistenceMode.Attribute)]
        public virtual string CodeBehindCssClass
        {
            get { return _codeBehindCssClass; }
            set { _codeBehindCssClass = value; }
        }


        private string _cssClass = "example"; // Default value.
        [PersistenceMode(PersistenceMode.Attribute)]
        public virtual string CssClass
        {
            get { return _cssClass; }
            set { _cssClass = value; }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public Example()
            : base()
        { }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        /*
        protected override void OnPreRender(EventArgs e)
        {
            if (Markup != null)
            {
                base.Controls.Add(base.Page.ParseControl(Markup));

                if (CodeBehind != null)
                {
                    // Generate dynamic code for the language specified by the "Language" attribute.
                    StringBuilder serverScript = new StringBuilder();
                    serverScript.Append("<script runat=\"server\" type=\"text/");

                    if (Language == ExampleLanguage.CSharp)
                    {
                        serverScript.Append("C#");
                    }
                    else if (Language == ExampleLanguage.VisualBasic)
                    {
                        serverScript.Append("VB");
                    }

                    serverScript.Append("\">").Append(CodeBehind).Append("</script>");

                    base.Controls.Add(base.Page.ParseControl(serverScript.ToString()));
                }
            }


            base.OnPreRender(e);
        }
        */


        protected override void Render(HtmlTextWriter writer)
        {
            if (Markup != null || CodeBehind != null)
            {
                writer.WriteLine("<div class=\"" + CssClass + "\">");
            }


            if (Markup != null)
            {
                    writer.WriteLine("<fieldset class=\"" + MarkupCssClass + "\">");
                        writer.Write("<legend>");
                            writer.Write(MarkupLabel);
                        writer.WriteLine("</legend>");
                        writer.WriteLine("<div class=\"code\">");
                            writer.WriteLine("<pre name=\"code\" class=\"brush:xhtml\">");
                                writer.WriteEncodedText(Markup);
                            writer.WriteLine("</pre>");
                        writer.WriteLine("</div>");
                    writer.WriteLine("</fieldset>");
            }


            if (CodeBehind != null)
            {
                    writer.WriteLine("<fieldset class=\"" + CodeBehindCssClass + "\">");
                        writer.Write("<legend>");
                            writer.Write(CodeBehindLabel);
                        writer.WriteLine("</legend>");
                        writer.WriteLine("<div class=\"code\">");
                            writer.WriteLine("<pre name=\"code\" class=\"brush:" + Language.ToString().ToLower() + "\">");
                                writer.WriteLine(CodeBehind);
                            writer.WriteLine("</pre>");
                        writer.WriteLine("</div>");
                    writer.WriteLine("</fieldset>");
            }


            if (Markup != null || CodeBehind != null)
            {
                writer.WriteLine("</div>");
            }
        }
        //------\\ Methods //-----------------------------------------------//
    }
}
